﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace FU.SE0208.G1.Controller
{
    public class CustomerHandler
    {
        // Store procedure to get order
        private const string STR_CUSTOMER_LIST = "Handler_Customer_List";

        public bool addnew(FU.SE0208.G1.Entities.Customer obj)
        {
            SqlConnection conn = DbHandler.getConn();
            try
            {
                conn.Open();
                SqlCommand command = new SqlCommand("AddCustomer", conn);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(new SqlParameter("@CusAdd", SqlDbType.NVarChar)).Value = obj.CusAdd1;
                command.Parameters.Add(new SqlParameter("@CusName", SqlDbType.NVarChar)).Value = obj.CusName1;
                command.Parameters.Add(new SqlParameter("@CusPhone", SqlDbType.NVarChar)).Value = obj.CusPhone1;
                command.Parameters.Add(new SqlParameter("@CusEmail", SqlDbType.NVarChar)).Value = obj.CusEmail1;
                command.Parameters.Add(new SqlParameter("@CusGender", SqlDbType.NVarChar)).Value = obj.CusGender1;
                return true;
            }
            catch (Exception ex) { return false; }
            finally { conn.Close(); conn.Dispose(); }
        }

        public bool del(FU.SE0208.G1.Entities.Customer obj)
        {
            SqlConnection conn = DbHandler.getConn();
            try
            {
                conn.Open();
                SqlCommand command = new SqlCommand("DeleteCustomer", conn);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(new SqlParameter("@CusID", SqlDbType.Int)).Value = Convert.ToInt32(obj.CusID1);
                int count = command.ExecuteNonQuery();
                if (count > 0)
                {
                    return true;
                }
                else
                    return false;
            }
            catch (Exception e) { return false; }
            finally { conn.Close(); conn.Dispose(); }
        }

        public bool edit(FU.SE0208.G1.Entities.Customer obj, FU.SE0208.G1.Entities.Customer newObj)
        {
            SqlConnection conn = DbHandler.getConn();
            try
            {
                conn.Open();
                SqlCommand command = new SqlCommand("EditOrder", conn);
                command.CommandType = CommandType.StoredProcedure;
                #region
                command.Parameters.Add(new SqlParameter("@CusID", SqlDbType.BigInt)).Value = Convert.ToInt64(obj.CusID1);
                command.Parameters.Add(new SqlParameter("@CusAdd", SqlDbType.NVarChar)).Value = newObj.CusAdd1;
                command.Parameters.Add(new SqlParameter("@CusName", SqlDbType.NVarChar)).Value = newObj.CusName1;
                command.Parameters.Add(new SqlParameter("@CusPhone", SqlDbType.NVarChar)).Value = newObj.CusPhone1;
                command.Parameters.Add(new SqlParameter("@CusEmail", SqlDbType.NVarChar)).Value = newObj.CusEmail1;
                command.Parameters.Add(new SqlParameter("@CusGender", SqlDbType.NVarChar)).Value = newObj.CusGender1;
                #endregion
                int count = command.ExecuteNonQuery();
                return true;
            }
            catch (Exception e) { return false; }
            finally { conn.Close(); conn.Dispose(); }
        }

        public DataSet getAll(string table)
        {
            DataSet ds = new DataSet("G1");
            DbHandler.getQuery(ds, "Select * from [" + table + "]", table);
            return ds;
        }

        public bool getCustomerList(DataSet ds, string sTable) 
        {
            if (DbHandler.getQuery(ds, STR_CUSTOMER_LIST, sTable) == false)
            {
                return false;
            }

            return true;
        }
    }
}
